# Built-In Example AI

# Title: guiwangzhengba
# Author: Adam Rumpf
# Version: 1.0.0
# Date: 11/12/2020
import math

import game.tcturtle

class CombatTurtle(game.tcturtle.TurtleParent):
    """Test circular combat turtle. 测试圆形战斗龟

    This turtle simply travels in circles and periodically shoots.这只海龟只是绕圈子，周期性地射击。
    """

    #-------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.静态方法返回战斗龟AI的名字。
        """

        return "ct_guiwangzhengba"

    #-------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.静态方法返回对海龟AI战斗的描述。
        """

        return "敌退我进，敌疲我打。"

    #-------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.用静态方法定义战斗龟的形状图像。

        The return value can be either an integer or a tuple of tuples.返回值可以是整数，也可以是元组的元组。

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star
            返回整数索引可选择以下预设形状之一：

0—箭头（对于无法识别的索引也是默认值）

1——海龟

2——犁

3——三角形

4--风筝

5——五角大楼

6——六边形

7--星形

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.可以通过返回表单的元组来定义自定义形状

（半径，角度），其中半径是半径的元组，角度是元组

描述多边形的极坐标的角度（弧度）

顶点。应该为海龟脸给出形状坐标

东方。
        """

        return 7

    #=========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.战斗龟的初始化代码。
        """

        # Define constant linear and angular speed factors
        self.nose_rel = (8, 0.0)  # just ahead of turtle's front
        self.hand_rel = (8, math.pi / 2)  # to left of turtle

    #-------------------------------------------------------------------------

    def step(self):
        """CombatTurtle.setup() -> None
        Step event code for Combat Turtle.
        """

        # Determine behavior based on whether there is line of sight 根据是否有视线来判断行为
        if (self.line_of_sight() == True):
            # If there is line of sight, move directly towards opponent 如果有视线，直接向对手移动

            # Turn towards opponent 转向对手
            self.turn_towards()

            # Move towards opponent (or away if too close) 向对手移动（如果距离太近则远离）
            if self.distance() > 4 * self.missile_radius:
                self.forward()
            else:
                self.backward()

            # Shoot if facing opponent 面对对手射击
            if (self.can_shoot == True and
                    abs(self.relative_heading_towards()) <= 10):
                self.shoot()

        else:
            # If no line of sight, attempt to navigate around obstacles 如果没有视线，尝试绕过障碍物

            # Calculate Cartesian coordinates of nose and hand 计算鼻子和手的笛卡尔坐标
            nose = ((self.x + self.nose_rel[0] *
                     math.cos(math.radians(self.heading) + self.nose_rel[1])),
                    (self.y - self.nose_rel[0] *
                     math.sin(math.radians(self.heading) + self.nose_rel[1])))
            hand = ((self.x + self.hand_rel[0] *
                     math.cos(math.radians(self.heading) + self.hand_rel[1])),
                    (self.y - self.hand_rel[0] *
                     math.sin(math.radians(self.heading) + self.hand_rel[1])))

            # Determine behavior based on whether nose and hand are clear 根据鼻子和手是否干净来判断行为
            if self.free_space(nose) == True:
                # Move forward when clear ahead 前方无障碍时向前移动
                self.forward()
            else:
                if self.free_space(hand) == True:
                    # If free to left, turn left 如果可以左转，就左转
                    self.left()
                    self.forward()
                else:
                    # If blocked ahead and to left, turn right 如果前方和左侧受阻，右转
                    self.right()
                    self.forward()
